package org.jeecg.modules.demo.storageoutindetail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.storageoutindetail.entity.StorageOutinDetail;
import org.jeecg.modules.demo.storageoutindetail.service.IStorageOutinDetailService;
import org.jeecg.modules.util.RedisUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 出入库明细表
 * @Author: jeecg-boot
 * @Date: 2022-02-14
 * @Version: V1.0
 */
@Api(tags = "出入库明细表")
@RestController
@RequestMapping("/storageoutindetail/storageOutinDetail")
@Slf4j
public class StorageOutinDetailController extends JeecgController<StorageOutinDetail, IStorageOutinDetailService> {
    @Autowired
    private IStorageOutinDetailService storageOutinDetailService;
    @Autowired
    private RedisUtils redisUtils;
    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**
     * 分页列表查询
     *
     * @param storageOutinDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "出入库明细表-分页列表查询")
    @ApiOperation(value = "出入库明细表-分页列表查询", notes = "出入库明细表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StorageOutinDetail storageOutinDetail,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }

        QueryWrapper<StorageOutinDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        QueryGenerator.initQueryWrapper(queryWrapper, storageOutinDetail, req.getParameterMap());
        Page<StorageOutinDetail> page = new Page<>(pageNo, pageSize);
        IPage<StorageOutinDetail> pageList = storageOutinDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 查询出入库明细列表（弃用）
     *
     * @param storageOutinDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "查询出入库明细列表")
    @ApiOperation(value = "查询出入库明细列表", notes = "查询出入库明细列表")
    @GetMapping(value = "/searchList")
    public Result<?> searchList(StorageOutinDetail storageOutinDetail,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                HttpServletRequest req) {

        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }

        QueryWrapper<StorageOutinDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        QueryGenerator.initQueryWrapper(queryWrapper, storageOutinDetail, req.getParameterMap());
        Page<StorageOutinDetail> page = new Page<>(pageNo, pageSize);
        IPage<StorageOutinDetail> pageList = storageOutinDetailService.searchList(page, storeId, storageOutinDetail.getCommodityName(),
                storageOutinDetail.getCommodityType(), storageOutinDetail.getBillNo(), storageOutinDetail.getOperateType(), beginTime, endTime);
        return Result.OK(pageList);
    }


    /**
     * 添加
     *
     * @param storageOutinDetail
     * @return
     */
    @AutoLog(value = "出入库明细表-添加")
    @ApiOperation(value = "出入库明细表-添加", notes = "出入库明细表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StorageOutinDetail storageOutinDetail, HttpServletRequest req) {
        //HttpServletRequest req
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        storageOutinDetail.setStoreId(storeId);
        storageOutinDetailService.save(storageOutinDetail);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param storageOutinDetail
     * @return
     */
    @AutoLog(value = "出入库明细表-编辑")
    @ApiOperation(value = "出入库明细表-编辑", notes = "出入库明细表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StorageOutinDetail storageOutinDetail) {
        storageOutinDetailService.updateById(storageOutinDetail);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "出入库明细表-通过id删除")
    @ApiOperation(value = "出入库明细表-通过id删除", notes = "出入库明细表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        storageOutinDetailService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "出入库明细表-批量删除")
    @ApiOperation(value = "出入库明细表-批量删除", notes = "出入库明细表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.storageOutinDetailService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "出入库明细表-通过id查询")
    @ApiOperation(value = "出入库明细表-通过id查询", notes = "出入库明细表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StorageOutinDetail storageOutinDetail = storageOutinDetailService.getById(id);
        if (storageOutinDetail == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(storageOutinDetail);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param storageOutinDetail
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StorageOutinDetail storageOutinDetail) {
        //return super.exportXls(request, storageOutinDetail, StorageOutinDetail.class, "出入库明细表");
        String token = TokenUtils.getTokenByRequest(request);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);

        // Step.1 组装查询条件
        QueryWrapper<StorageOutinDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        QueryGenerator.initQueryWrapper(queryWrapper, storageOutinDetail, request.getParameterMap());
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //update-end--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据----------------------
        List<StorageOutinDetail> pageList = storageOutinDetailService.list(queryWrapper);

        // Step.3 AutoPoi 导出Excel
        mv.addObject(NormalExcelConstants.FILE_NAME, "出入库明细表"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, StorageOutinDetail.class);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams("出入库明细表数据", "导出人:" + user.getRealname(), "出入库明细表");
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StorageOutinDetail.class);
    }

}
